<?php namespace sacvoyage\hook;

require_once('sacvoyage/hook/Hook.php');
use \sacvoyage\hook\Hook;

require_once('sacvoyage/Object.php');
use \sacvoyage\Object;

require_once('sacvoyage/domain/Collection.php');
use \sacvoyage\domain\Collection;

class ShareHook extends Hook {
	

	
	public function onWrite(Collection $collection, $documents) {
		if(!is_enumerative_array($documents) && !$documents instanceof \Iterator){
			$documents=array($documents);
		}
		// user adds share-config to own sacvoyage
		if(self::$catch_user_write && $collection->getName()=='sys.share') {
			self::$catch_root_write=false;
			
			$owner=$collection->getOwner();
			$root=$collection->getOwner()->getDomain()->loadUser('root');
			$allShares=$root->getProperty()->getCollection('sys.user.share');
			
			foreach($documents as $document) {
				if(isset($document->_id)
				&& ($config=$allShares->read($document->_id))
				&& ($config->owner!=$owner->getName())) {
					throw new Exception('this share name is already taken');
				} else {
					$document->owner=$collection->getOwner()->getName();
					$allShares->write($document);
				}
			}
			
			self::$catch_root_write=true;
		} 
		
		// root adds share-config to user account
		elseif(self::$catch_root_write && $collection->getOwner()->getName()=='root' 
		&& $collection->getName()=='sys.user.share') {
			self::$catch_user_write=false;
			$domain=$collection->getOwner()->getDomain();
			foreach($documents as $document) {
				if(isset($document->owner)) {
					$owner=$domain->loadUser($document->owner);
					$ownCollection=$owner->getProperty()->getCollection('sys.share');
					$ownCollection->write($document);
				}
			}
			self::$catch_user_write=true;
		}
		
		return true;
	}
	
	public function onDelete($collection, $query) {
		// $rev==null means completely deleting, 
		// $rev!=null means versioned deletion
		
		// user deletes share-config from own sacvoyage
		if(self::$catch_user_delete && $collection->getName()=='sys.share') {
			self::$catch_root_delete=false;
			$owner=$collection->getOwner();
			$root=$owner->getDomain()->loadUser('root');
			$allShares=$root->getProperty()->getCollection('sys.user.share');
			
			if(!$query) {
				$query=new Object(array());
			}
			if(!isset($query->filter)) {
				$query->filter=new Object(array());
			}
			$query->filter->owner=$owner->getName();
			$allShares->delete($query);
			self::$catch_root_delete=true;
		} 
		// root adds share-config to user account
		elseif(self::$catch_root_delete && $collection->getOwner()->getName()=='root' 
		&& $collection->getName()=='sys.user.share') {
			self::$catch_user_delete=false;
			$domain=$collection->getOwner()->getDomain();
			$documents=$collection->find($query);
			foreach($documents as $document) {
				if(isset($document->owner)) {
					$owner=loadUser($document->owner);
					$ownCollection=$owner->getCollection('sys.share');
					$ownCollection->delete($query);	
				}				
			}
			self::$catch_user_delete=true;
		}
		return true;
	}
	
	private static $catch_user_write=true;
	private static $catch_root_write=true;
	
	private static $catch_user_delete=true;
	private static $catch_root_delete=true;
	
}



?>